#include "MenuScene.h"
#include "GameScene.h"
#include "Block.h"
USING_NS_CC;

Scene* MenuScene::createScene()
{
    return MenuScene::create();
}

bool MenuScene::init()
{

	// new version 1.1

    if ( !Scene::init() )
    {
        return false;
    }

	Dictionary * dic = Dictionary::createWithContentsOfFile("sss.plist");
	
	auto ele = dic->valueForKey("metadata")->getCString();
	
	auto arr = dic->allKeys();

	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("blocks.plist");

    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

	m_map = TMXTiledMap::create("map/map_level_1.tmx");
	this->addChild(m_map);

	m_player = Block::create();
	m_player->setPosition(Vec2(100, 100));
	m_map->addChild(m_player);




	scheduleUpdate();
    return true;
}


void MenuScene::update(float dt)
{
	auto visibleSize = Director::getInstance()->getVisibleSize();
	auto pos = m_player->convertToWorldSpace(Vec2::ZERO);

	auto offset = Vec2(visibleSize.width/2 - pos.x, visibleSize.height/2 - pos.y);

	m_map->setPosition(m_map->getPosition() + offset);

	if (m_map->getPositionX() > 0) {

		m_map->setPositionX(0);
	}
	if (m_map->getPositionX() < -(m_map->getMapSize().width * m_map->getTileSize().width - visibleSize.width)) {

		m_map->setPositionX(-(m_map->getMapSize().width * m_map->getTileSize().width - visibleSize.width));
	}
	//if (m_map->getPositionY() > 0) {

	//	m_map->setPositionY(0);
	//}
	//if (m_map->getPositionY() < m_map->getMapSize().height) {

	//	m_map->setPositionY(m_map->getMapSize().height);
	//}

	log("%f, %f\n", offset.x, offset.y);
}
